package com._119yn.host.gst200.service.impl;

import com._119yn.host.gst200.constant.LogTags;
import com._119yn.host.gst200.model.ZngkPlatformSignal;
import com._119yn.host.gst200.properties.DadsProperties;
import com._119yn.host.gst200.service.DadsService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;

@Slf4j
@Service
@RequiredArgsConstructor
public class DadsServiceImpl implements DadsService {

    private final static String DATA_API_PATH = "/dataStation/dataUpstream/data/";

    private final DadsProperties dadsProperties;

    private final RestClient restClient;

    private final ObjectMapper objectMapper;

    @Override
    public void dtuNetworkState(ZngkPlatformSignal signal) throws RuntimeException {
        String uri = dadsProperties.getUrl().concat(DATA_API_PATH).concat("dtuNetworkState");
        try {
            String json = objectMapper.writeValueAsString(signal);
            logInfo(LogTags.getDadsClient(), "网络状态", json);
            String resp = restClient
                    .post()
                    .uri(uri)
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(json)
                    .retrieve()
                    .body(String.class);
            logInfo(LogTags.getDadsClient(), "响应信息", resp);
        } catch (Exception e) {
            logWarn(LogTags.getDadsClient(), "异常信息", e.getMessage());
            throw new RuntimeException("汇报网络状态异常");
        }
    }

    @Override
    public void update(ZngkPlatformSignal signal) throws RuntimeException {
        String uri = dadsProperties.getUrl().concat(DATA_API_PATH).concat("update");
        try {
            String json = objectMapper.writeValueAsString(signal);
            logInfo(LogTags.getDadsClient(), "设备状态", json);
            String resp = restClient
                    .post()
                    .uri(uri)
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(json)
                    .retrieve()
                    .body(String.class);
            logInfo(LogTags.getDadsClient(), "响应信息", resp);
        } catch (Exception e) {
            logWarn(LogTags.getDadsClient(), "异常信息", e.getMessage());
            throw new RuntimeException("更新设备状态异常");
        }
    }

    @Override
    public void registers(ZngkPlatformSignal signal) throws RuntimeException {
        String uri = dadsProperties.getUrl().concat(DATA_API_PATH).concat("registers");
        try {
            String json = objectMapper.writeValueAsString(signal);
            logInfo(LogTags.getDadsClient(), "设备注册", json);
            String resp = restClient
                    .post()
                    .uri(uri)
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(json)
                    .retrieve()
                    .body(String.class);
            logInfo(LogTags.getDadsClient(), "响应信息", resp);
        } catch (Exception e) {
            logWarn(LogTags.getDadsClient(), "异常信息", e.getMessage());
            throw new RuntimeException("执行设备注册异常");
        }
    }

    private void logInfo(String tag, String name, String content) {
        log.info("{}:[{}]: {}", tag, name, content);
    }

    private void logWarn(String tag, String name, String content) {
        log.warn("{}:[{}]: {}", tag, name, content);
    }

}
